// pages/appiointChoose/appiointChoose.js
const app = getApp()
const api = require("../../utils/api")
const util = require("../../utils/util")
Page({
  /**
   * 页面的初始数据
   */
  data: {
    TabCur: 0,
    deptList: [],
    deptFilterList: [],
    doctorList: [],
    doctorFilterList: [],
    searchDeptName: '',
    searchDrname: '',
    schedulingList: []
  },
  tabSelect(e) {
    this.setData({
      TabCur: e.currentTarget.dataset.id
    })
  },
  handleDeptClick(event){
    let that = this
    wx.navigateTo({
      url: '/pages/appiointChoose/dateAndDoctorChoose?deptCode='+event.currentTarget.dataset.target,
      success: function(res) {
        // 通过eventChannel向被打开页面传送数据
        res.eventChannel.emit('schedulingList', { data: that.data.schedulingList })
      }
    })
  },
  handleDoctorClick(event){
    let that = this
    wx.navigateTo({
      url: '/pages/appiointChoose/dateAndDoctorChoose?drCode='+event.currentTarget.dataset.target,
      success: function(res) {
        // 通过eventChannel向被打开页面传送数据
        res.eventChannel.emit('schedulingList', { data: that.data.schedulingList })
      }
    })
  },
  handleDeptSearch(event){
   let filters= this.data.deptFilterList.filter(m=>
      m.deptName.indexOf(this.data.searchDeptName.toLowerCase()) > -1 || m.deptPinyin.indexOf(this.data.searchDeptName.toLowerCase()) > -1
    );
    this.data.deptList.splice(0,this.data.deptList.length);
    filters.forEach(m=>{
      this.data.deptList.push(m)
    })
    this.setData({
      deptList: this.data.deptList
    })
  },
  handleDoctorSearch(event){
    let filters= this.data.doctorFilterList.filter(m=>
      m.drName.indexOf(this.data.searchDrname.toLowerCase()) > -1 || m.drPinyin.indexOf(this.data.searchDrname.toLowerCase()) > -1
    );
    this.data.doctorList.splice(0,this.data.doctorList.length);
    filters.forEach(m=>{
      this.data.doctorList.push(m)
    })
    this.setData({
      doctorList: this.data.doctorList
    })
  },
  PageLoadData(){
    let days = app.globalData.schedulingDays
    var startDt = new Date();//开始时间
    var endDt = new Date();//结束时间
    endDt=endDt.setDate(endDt.getDate()+days);
    endDt=new Date(endDt);

    const that = this;
    api.getDoctorSchedulingList( {
      startDt: startDt.toLocaleDateString().replace(new RegExp("/","gm"),'-'),
      endDt: endDt.toLocaleDateString().replace(new RegExp("/","gm"),'-')
    }).then(res => {
      res = res.filter(m=> util.filterScheduling(m.scheDate,m.scheAmEnd,m.schePmEnd,m.scheEveEnd,m.scheAmState,m.schePmState,m.scheEveState));
      console.log(res)
      that.data.deptList.splice(0,that.data.deptList.length);
      that.data.deptFilterList.splice(0,that.data.deptFilterList.length);
      that.data.doctorList.splice(0,that.data.doctorList.length);
      that.data.doctorFilterList.splice(0,that.data.doctorFilterList.length);
      res.forEach(item => {
        if(that.data.deptList.findIndex(m=>m.deptCode==item.scheDeptCode)<0){
          var dept = {
            deptCode: item.scheDeptCode,
            deptPinyin: item.deptPinyin,
            deptName: item.deptName
          }
          that.data.deptList.push(dept)
          that.data.deptFilterList.push(dept)
        }
        if(that.data.doctorList.findIndex(m=>m.drCode == item.scheDrCode)<0){
          var doctor = {
            drCode: item.scheDrCode,
            drName: item.drName,
            drPinyin: item.drPinyin,
            level: item.drLevelName,
            image: api.getBaseUrl()+item.drPhoto
          }
          that.data.doctorList.push(doctor);
          that.data.doctorFilterList.push(doctor);
        }
        var scheduling = {
          scheId: item.scheId,
          deptCode: item.scheDeptCode,
          deptName: item.deptName,
          deptPinyin: item.deptPinyin,
          drCode: item.scheDrCode,
          drName: item.drName,
          drPinyin: item.drPinyin,
          level: item.drLevelName,
          image: api.getBaseUrl()+item.drPhoto,
          registerNo: util.getRegisterNoNumber(item.scheDate,item.scheAmEnd,item.schePmEnd, item.scheEveEnd,item.scheAmNumber,item.schePmNumber, item.scheEveNumber, item.scheAmState, item.schePmState, item.scheEveState),
          date: util.timestampToLocalFormat(item.scheDate),
          introduce: item.introduce,
          fee: item.fee,
          childFee: item.childFee
        }
        that.data.schedulingList.push(scheduling)
      });
     that.setData({
      deptList: that.data.deptList,
      doctorList: that.data.doctorList
     })
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.PageLoadData()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})